/**
 *
 */
package datastructure.sort;

import java.util.Comparator;

/**
 * Description：冒泡排序
 * 每次两两之间比较，直到达到最终排序结果
 * 优化：当一次排队过程没有发生元素交换时，排序完成
 * Author: zhangc
 * Date：2017/6/19 21:13
 */
public final class BubbleSort {

    public static <T extends Comparable<? super T>> void bubbleSortAble(T[] array) {
        T temp;
        boolean status = true;
        for (int i = 0; i < array.length && status; ++i) {
            status = false;// 每一轮都要重新设置标志位
            for (int j = 1; j < array.length; ++j)
                if (array[ j - 1 ].compareTo(array[ j ]) < 0) {
                    temp = array[ j - 1 ];
                    array[ j - 1 ] = array[ j ];
                    array[ j ] = temp;
                    status = true;
                }
        }
    }

    public static <T> void bubbleSortOr(T[] array, Comparator<? super T> comparor) {
        T temp;
        boolean status = true;
        for (int i = 0; i < array.length && status; ++i) {
            status = false;// 每一轮都要重新设置标志位
            for (int j = 1; j < array.length; ++j) {
                if (comparor.compare(array[ j - 1 ], array[ j ]) < 0) {
                    temp = array[ j - 1 ];
                    array[ j - 1 ] = array[ j ];
                    array[ j ] = temp;
                    status = true;
                }
            }
        }
    }

}
